<template>
	<view style="height: 100%;">
		<swiper-tabs height="100%">
			<template v-slot:tabs style="height: 100%;">
				<view style="height: 100%;">
					<u-tabs-swiper name="name" barWidth="240" active-color="#333333" inactive-color="#666666" bg-color="none"
						ref="tabs1" :list="parames.tabs" :current="current" @change="tabsChange" :is-scroll="false"
						swiperWidth="750"></u-tabs-swiper>
					<swiper style="height: calc(100% - 84rpx);" :current="swiperCurrent" @transition="transition"
						@animationfinish="animationfinish" class="swiper">
						<slot name="swiperTbs'"></slot>
						<swiper-item :style="swiperItemStyle" class="swiper-item" v-for="(item, index) in parames.tabs" :key="index">
							<scroll-view scroll-y class="scroll" @scrolltolower="loadmore">
								<card @reset="reset" :list="parames.list" v-if="parames.list.length" :swiperCurrent="swiperCurrent" />
								<view class="def" v-else>
									<view style="margin: 120rpx 0 80rpx 153rpx;">您目前没有申请添加朋友</view>
									<view class="def-btn" style="margin-left: 57rpx;" @click="applyNow">即刻申请</view>
								</view>
								<u-loadmore bg-color="none" :status="parames.loadStatus" @loadmore="loadmore" v-if="parames.list.length"></u-loadmore>
							</scroll-view>
						</swiper-item>
					</swiper>
					
				</view>
			</template>
		</swiper-tabs>
	</view>
</template>

<script lang="ts" setup>
	import swiperTabs from '@/components/swiperTabs.vue'
	import card from './components/card.vue'
	import {
		reactive,ref,watch
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import me from '../../../api/me';
	let tabs1 = ref()
	let current = ref(0)
	let swiperCurrent = ref(0)
	let parames = reactive({
		list: [],
		tabs: [
			{
				name: '新的朋友申请'
			}, {
				name: '我添加的朋友'
			}
		],
		page: 1,
		loadStatus: '',
		isOpen: true
	})
	watch(() => swiperCurrent.value, (index) => {
		if(parames.isOpen) {
			reset()
		}
		parames.isOpen = true
	})
	const reset = () => {
		parames.list = []
		parames.page = 1
		getFriendOrder()
	}
	const loadmore = () => {
		if(parames.loadStatus == 'nomore') return
		getFriendOrder()
	}
	const applyNow = () => {
		uni.switchTab({
			url: '/pages/tabs/networking/index'
		})
	}
	const moreStatus = (status: string) => {
		parames.loadStatus = status
	}
	// 列表
	const getFriendOrder = async () => {
		const res = await me.getFriendOrder({
			type: swiperCurrent.value + 1,
			size: 10,
			page: parames.page
		})
		res.data.map(v => {
			v.show = false
			v.timestamp = differenceTime(v.createtime || 0)*1000
		})
		parames.list = [...parames.list,...res.data]
		if (res.data.length < 10) return moreStatus('nomore')
		if (res.data.total <= 10) {
			moreStatus('nomore')
		} else {
			moreStatus(res.data.length !== 0 ? 'loadmore' : 'nomore')
		}
		if (res.data.length === 0) return false
		parames.page += 1 // 当前页加1
	}
	const differenceTime = (time: any) => {
		// @ts-ignore
		let timestamp: any = (Date.parse(new Date())) / 1000; //计算当前时间戳
		// @ts-ignore
		let pastTime: any  = (Date.parse(new Date(time))) / 1000 //创建时间
		let endtime = pastTime + 1800
		return endtime > timestamp ? endtime - timestamp : 0
	}
	// tabs通知swiper切换
	const tabsChange = (index : number) => {
		swiperCurrent.value = index;
	}
	// swiper-item左右移动，通知tabs的滑块跟随移动
	const transition = (e) => {
		let dx = e.detail.dx;
		tabs1.value.setDx(dx);
	}
	const animationfinish = (e) => {
		let current2 = Number(e.detail.current)
		tabs1.value.setFinishCurrent(current2);
		swiperCurrent.value = current2;
		current.value = current2;
	}
	onLoad((op) => {
		if(op.index) {
			swiperCurrent.value = Number(op.index)
			current.value = swiperCurrent.value
			parames.isOpen = false
		}
		getFriendOrder()
	})
</script>

<style>
	page{
		height: 100%;
		
	}
</style>
<style lang="scss">
	.def {
		width: 654rpx;
		height: 393rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 16rpx 3rpx rgba(225, 225, 225, 0.5);
		border: 2rpx solid #EEEFF0;
		position: absolute;
		top: 200rpx;
		left: 48rpx;
	
		.def-btn {
			text-align: center;
			width: 544rpx;
			height: 92rpx;
			background: #000000;
			line-height: 92rpx;
			color: #fff;
		}
	}
</style>